<template>
  <el-row :gutter="40">
    <el-col v-if="!form.id">
      <el-empty description="请选择左侧菜单后操作" :image-size="100"></el-empty>
    </el-col>
    <template v-else>
      <el-col :lg="12">
        <h2>{{ form.meta.title || "新增菜单" }}</h2>
        <el-form
          :model="form"
          :rules="rules"
          ref="dialogForm"
          label-width="80px"
          label-position="right"
          class="noFlex"
        >
          <el-form-item label="显示名称" prop="meta.title">
            <el-input
              v-model="form.meta.title"
              clearable
              placeholder="菜单显示名字"
            ></el-input>
          </el-form-item>
          <el-form-item label="上级菜单" prop="parentId">
            <el-cascader
              v-model="form.parentId"
              :options="menuOptions"
              :props="menuProps"
              :show-all-levels="false"
              placeholder="顶级菜单"
              clearable
              disabled
            ></el-cascader>
          </el-form-item>
          <el-form-item label="类型" prop="meta.type">
            <el-radio-group v-model="form.meta.type">
              <el-radio-button label="menu">菜单</el-radio-button>
              <el-radio-button label="iframe">Iframe</el-radio-button>
              <el-radio-button label="link">外链</el-radio-button>
              <el-radio-button label="button">按钮</el-radio-button>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="别名" prop="name">
            <el-input
              v-if="form.meta.type != 'button'"
              v-model="form.path"
              clearable
              placeholder="菜单别名"
              disabled
            ></el-input>
            <el-input
              v-else
              v-model="form.path"
              clearable
              placeholder="按钮别名"
            ></el-input>
            <div class="el-form-item-msg" v-if="form.meta.type != 'button'">
              系统唯一且与内置组件名一致，否则导致缓存失效。如类型为Iframe的菜单，别名将代替源地址显示在地址栏
            </div>
            <div class="el-form-item-msg" v-else>
              如：add、update、delete、deleteList、edit、show、reset、set、check、submitCheck
            </div>
          </el-form-item>
          <el-form-item
            label="菜单图标"
            prop="meta.icon"
            v-if="form.meta.type != 'button'"
          >
            <sc-icon-select v-model="form.meta.icon" clearable></sc-icon-select>
          </el-form-item>
          <el-form-item
            label="路由地址"
            prop="path"
            v-if="form.meta.type != 'button'"
          >
            <el-input v-model="form.path" clearable placeholder=""></el-input>
          </el-form-item>
          <el-form-item label="是否隐藏">
            <!-- <el-switch v-model="form.meta.enabled" /> -->
            <el-switch
              v-model="form.meta.hidden"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </el-form-item>
          <el-form-item label="是否开启">
            <!-- <el-switch v-model="form.meta.enabled" /> -->
            <el-switch
              v-model="form.meta.enabled"
              :active-value="1"
              :inactive-value="0"
            ></el-switch>
          </el-form-item>
          <el-form-item
            label="排序"
            style="width: 100%"
            v-if="form.meta.type != 'button'"
          >
            <el-input-number
              v-model="form.meta.sort"
              controls-position="right"
              :min="0"
            ></el-input-number>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="save" :loading="loading"
              >保 存</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
      <el-col :lg="12" class="apilist">
        <h2>接口权限</h2>
        <sc-form-table
          v-model="form.apiList"
          :addTemplate="apiListAddTemplate"
          placeholder="暂无匹配接口权限"
        >
          <el-table-column prop="code" label="标识" width="150">
            <template #default="scope">
              <el-input
                v-model="scope.row.code"
                placeholder="请输入内容"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="url" label="Api url">
            <template #default="scope">
              <el-input
                v-model="scope.row.url"
                placeholder="请输入内容"
              ></el-input>
            </template>
          </el-table-column>
        </sc-form-table>
      </el-col>
    </template>
  </el-row>
</template>

<script>
import scIconSelect from '@/components/scIconSelect'

export default {
  components: {
    scIconSelect
  },
  props: {
    menu: { type: Object, default: () => { } },
  },
  data () {
    return {
      form: {
        id: "",
        parentId: "",
        name: "",
        path: "",
        component: "",
        meta: {
          title: "",
          icon: "",
          type: "menu",
          enabled: 1,
          sort: 0,
          hidden: 0
        }
      },
      menuOptions: [],
      menuProps: {
        value: 'id',
        label: 'title',
        checkStrictly: true
      },
      predefineColors: [
        '#ff4500',
        '#ff8c00',
        '#ffd700',
        '#67C23A',
        '#00ced1',
        '#409EFF',
        '#c71585'
      ],
      rules: [],
      apiListAddTemplate: {
        code: "",
        url: ""
      },
      loading: false
    }
  },
  watch: {
    menu: {
      handler () {
        this.menuOptions = this.treeToMap(this.menu)
      },
      deep: true
    }
  },
  mounted () {
  },
  methods: {
    //简单化菜单
    treeToMap (tree) {
      const map = []
      tree.forEach(item => {
        var obj = {
          id: item.id,
          parentId: item.parentId,
          title: item.meta.title,
          children: item.children && item.children.length > 0 ? this.treeToMap(item.children) : null
        }
        map.push(obj)
      })
      return map
    },
    //保存
    async save () {
      console.log(this.form)
      this.loading = true
      // type:0:菜单;1:iframe;2:外连，3：按钮
      let type = ''
      switch (this.form.meta.type) {
        case 'menu':
          type = 0
          break
        case 'iframe':
          type = 1
          break
        case 'link':
          type = 2
          break
        case 'button':
          type = 3
          break

      }
      let newMenu = {
        enabled: this.form.meta.enabled,
        icon: this.form.meta.icon,
        path: this.form.path,
        parentId: this.form.parentId,
        id: this.form.id,
        title: this.form.meta.title,
        type: type,
        sort: this.form.meta.sort,
        hidden: this.form.meta.hidden
      }
      var res = await this.$API.menu.updateMenu.post(newMenu)
      this.loading = false
      if (res.code == 200) {
        this.$message.success("保存成功")
      } else {
        this.$message.warning(res.message)
      }
    },
    //表单注入数据
    setData (data, pid) {
      console.log(data)
      this.form = data
      this.form.apiList = data.apiList || []
      this.form.parentId = pid
    }
  }
}
</script>

<style scoped>
h2 {
  font-size: 17px;
  color: #3c4a54;
  padding: 0 0 30px 0;
}
.apilist {
  border-left: 1px solid #eee;
}

[data-theme="dark"] h2 {
  color: #fff;
}
[data-theme="dark"] .apilist {
  border-color: #434343;
}
</style>
